<?php
declare(strict_types=1);


namespace App\Mall\Mapper;

use App\Mall\Model\MallCategory;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use Mine\MineCollection;

/**
 * 分类列表Mapper类
 */
class MallCategoryMapper extends AbstractMapper
{
    /**
     * @var MallCategory
     */
    public $model;

    public function assignModel()
    {
        $this->model = MallCategory::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query;
    }

    public function tree(): array
    {
        $query = $this->model::query();
        $res = new MineCollection($query->get()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => fastapp_lang_text($item->name),
                'parent_id' => $item->parent_id,
                'value' => $item->id,
            ];
        }));
        return $res->toTree();
    }

    public function select(): array
    {
        return $this->model::query()->get()->map(function ($item) {
            return [
                'id' => $item->id,
                'label' => fastapp_lang_text($item->name),
                'value' => $item->id,
            ];
        })->toArray();
    }
}